from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

import sys

sys.path.append(r'..')
from web_base.base_page import BasePage
from page_object.login_page import LoginPage


class AssetPage(BasePage):
    url = r'/bsams/front/asset/asset_list.do'
    # 新增定位
    add_btn_loc = (By.XPATH, '/html/body/div[2]/div/div[2]/div[2]/div[2]/div/input')
    add_title_loc = (By.ID, 'title')
    add_providerId_loc = (By.ID, 'providerId')
    add_storageId_loc = (By.ID, 'storageId')
    add_ok_loc = (By.CLASS_NAME, 'button_fujian.margin')
    # 修改定位
    mod_btn_loc = (By.LINK_TEXT, '休改')
    mod_title_loc = (By.ID, 'title')
    mod_providerId_loc = (By.ID, 'providerId')
    mod_storageId_loc = (By.ID, 'storageId')
    mod_ok_loc = (By.XPATH, '//*[@id="fm_new"]/div/div[1]')

    # 搜索资产search
    search_status_loc = (By.ID, 'status')
    search_title_loc = (By.ID, 'title')
    search_ok_loc = (By.XPATH, '//*[@id="fmsearch"]/div[3]/button')
    # 资产借用loan
    loan_btn_loc = (By.LINK_TEXT, '借用')
    loan_departId_loc = (By.ID, 'departId')
    loan_remark_loc = (By.ID, 'remark')
    loan_ok_loc = (By.XPATH, '//*[@id="fm_new"]/div/div[1]')
    # 资产归还
    return_btn_loc = (By.LINK_TEXT, '归还')
    return_ok_loc = (By.XPATH, '//*[@id="cboxLoadedContent"]/div/div/div[2]/div[2]/div[1]')

    def asset_add(self, title, providerId, storageId):
        # 打开资产管理页面
        self.open()
        self.ele_click(self.add_btn_loc)
        self.input_ele(self.add_title_loc, title)
        self.ele_select(self.add_providerId_loc, providerId)
        self.ele_select(self.add_storageId_loc, storageId)
        self.ele_click(self.add_ok_loc)
        sleep(3)
        # 获取警告弹窗的内容
        text = self.get_alert_text()
        self.alert_click()
        return text

    def asset_mod(self, title, providerId, storageId):
        # 打开资产管理页面
        self.open()
        # 资产修改
        self.ele_click(self.mod_btn_loc)
        self.ele_clear(self.mod_title_loc)
        self.input_ele(self.add_title_loc, title)
        self.ele_select(self.add_providerId_loc, providerId)
        self.ele_select(self.add_storageId_loc, storageId)
        self.ele_click(self.add_ok_loc)
        sleep(3)
        # 获取警告弹窗的内容
        text = self.get_alert_text()
        self.alert_click()
        return text

    def asset_search(self, status, title):
        # 打开资产管理页面
        self.open()
        # 搜索一下
        self.ele_select(self.search_status_loc, status)
        self.ele_clear(self.search_title_loc)
        self.input_ele(self.search_title_loc, title)
        self.ele_click(self.search_ok_loc)
        sleep(3)

    def asset_loan(self, departId, remark):
        # 打开资产管理页面
        self.open()
        # 资产借用
        self.ele_click(self.loan_btn_loc)
        self.ele_select(self.loan_departId_loc, departId)
        self.ele_clear(self.loan_remark_loc)
        self.input_ele(self.loan_remark_loc, remark)
        self.ele_click(self.loan_ok_loc)
        sleep(3)
        # 获取警告弹窗的内容
        text = self.get_alert_text()
        self.alert_click()
        return text

    def asset_return(self):
        # 资产归还
        self.open()
        self.ele_click(self.return_btn_loc)
        self.ele_click(self.return_ok_loc)
        sleep(3)
        # 获取警告弹窗的内容
        text = self.get_alert_text()
        self.alert_click()
        return text


if __name__ == '__main__':
    providerId = '维信科技发展有限公司'
    storageId = '电脑设备库'

    chrome_location = r'E:\python_pro\Win_x64_1135105_chrome-win\chrome-win\chrome.exe'
    options = webdriver.ChromeOptions()
    options.binary_location = chrome_location

    driver = webdriver.Chrome(options=options)

    login_page = LoginPage(driver)
    login_page.login()

    # 测试新增
    # asset_page = AssetPage(driver)
    # text = asset_page.asset_add(title, providerId, storageId)
    # print(text)

    # # 测试修改
    asset_page = AssetPage(driver)
    # text = asset_page.asset_mod(title, providerId, storageId)
    # print(text)
    # 测试搜索
    # status = '使用'
    # title = '华为001'
    # asset_page.asset_search(status, title)

    # 借用
    departId = '软件部'
    remark = '123456789'
    asset_page.asset_loan(departId, remark)

    driver.quit()
